/*
 * Created by JFormDesigner on Sun May 18 22:57:36 GMT+08:00 2025
 */

package com.soft.bams.client;

import com.soft.bams.entity.Account;
import com.soft.bams.exception.LoginException;
import com.soft.bams.exception.NotFoundException;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * @author wzs
 */
public class LoginPanel extends JPanel {

    private ATMClient atm;

    public LoginPanel(ATMClient atm) {
        this.atm = atm;
        initComponents();
    }

    //确认按钮
    private void okButtonActionPerformed(ActionEvent e) {
        //收集登录界面数据
        String idStr = textField.getText();
        String password = new String(passwordField.getPassword());

        //执行Bank中登录操作
        if(idStr!=null && !idStr.equals("")){
            Long id = Long.valueOf(idStr);
            try {
                Account acc = atm.bank.login(id, password);
                //登陆成功
                if(acc!=null){
                    //初始化BusinessPanel界面数据
                    atm.acc = acc;
                    atm.businessPanel.initBusiness();

                    //直接跳转至BusinessPanel界面
                    CardLayout cardLayout = (CardLayout) this.getParent().getLayout();
                    cardLayout.show(this.getParent(),"businessPanel");

                    //清空登录界面数据
                    clearLogin();
                }
            } catch (LoginException | NotFoundException ex) {
                //登录失败，则弹框显示失败信息，并清空密码框
                JOptionPane.showMessageDialog(null,ex.getMessage());
                passwordField.setText("");
            }
        }else{
            JOptionPane.showMessageDialog(null,"账户不能为空！");
        }
    }

    //点击取消按钮，回到主界面
    private void cancelButtonActionPerformed(ActionEvent e) {
        CardLayout cardLayout = (CardLayout) this.getParent().getLayout();
        cardLayout.show(this.getParent(),"mainPanel");

        //清空登录界面
        clearLogin();
    }

    //清空登陆界面
    public void clearLogin(){
        textField.setText("");
        passwordField.setText("");
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        titleLabel = new JLabel();
        cancelButtonActionPerformed = new JButton();
        okButtonActionPerformed = new JButton();
        idLabel = new JLabel();
        pwdLabel = new JLabel();
        textField = new JTextField();
        passwordField = new JPasswordField();

        //======== this ========
        setLayout(null);

        //---- titleLabel ----
        titleLabel.setText("\u767b \u5f55 \u754c \u9762");
        titleLabel.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 20));
        titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
        add(titleLabel);
        titleLabel.setBounds(180, 40, 185, 45);

        //---- cancelButtonActionPerformed ----
        cancelButtonActionPerformed.setText("\u53d6\u6d88");
        cancelButtonActionPerformed.addActionListener(e -> cancelButtonActionPerformed(e));
        add(cancelButtonActionPerformed);
        cancelButtonActionPerformed.setBounds(380, 260, 105, 35);

        //---- okButtonActionPerformed ----
        okButtonActionPerformed.setText("\u786e\u8ba4");
        okButtonActionPerformed.addActionListener(e -> okButtonActionPerformed(e));
        add(okButtonActionPerformed);
        okButtonActionPerformed.setBounds(85, 260, 105, 35);

        //---- idLabel ----
        idLabel.setText("\u7528 \u6237 \u8d26 \u53f7");
        idLabel.setHorizontalAlignment(SwingConstants.CENTER);
        add(idLabel);
        idLabel.setBounds(85, 110, 100, 35);

        //---- pwdLabel ----
        pwdLabel.setText("\u7528 \u6237 \u5bc6 \u7801");
        pwdLabel.setHorizontalAlignment(SwingConstants.CENTER);
        add(pwdLabel);
        pwdLabel.setBounds(85, 170, 100, 35);
        add(textField);
        textField.setBounds(220, 110, 220, 35);
        add(passwordField);
        passwordField.setBounds(220, 170, 220, 35);

        setPreferredSize(new Dimension(545, 390));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel titleLabel;
    private JButton cancelButtonActionPerformed;
    private JButton okButtonActionPerformed;
    private JLabel idLabel;
    private JLabel pwdLabel;
    private JTextField textField;
    private JPasswordField passwordField;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
